class Solution {
public:
    int distributeCandies(int n, int limit) {
        int ret=0;

        for(int i=0;i<=min(n,limit);i++)
        {
            if(n-i>2*limit)
                continue;
            ret+=min(n-i,limit)-max(0,n-i-limit)+1;
        }

        return ret;
    }
};
